package com.java110.mall.shop.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.shop.ShopSettingDto;
import com.java110.po.shop.ShopSettingPo;
import com.java110.mall.shop.bmo.shopSetting.IDeleteShopSettingBMO;
import com.java110.mall.shop.bmo.shopSetting.IGetShopSettingBMO;
import com.java110.mall.shop.bmo.shopSetting.ISaveShopSettingBMO;
import com.java110.mall.shop.bmo.shopSetting.IUpdateShopSettingBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/shopSetting")
public class ShopSettingApi {

    @Autowired
    private ISaveShopSettingBMO saveShopSettingBMOImpl;
    @Autowired
    private IUpdateShopSettingBMO updateShopSettingBMOImpl;
    @Autowired
    private IDeleteShopSettingBMO deleteShopSettingBMOImpl;

    @Autowired
    private IGetShopSettingBMO getShopSettingBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopSetting/saveShopSetting
     * @path /app/shopSetting/saveShopSetting
     */
    @RequestMapping(value = "/saveShopSetting", method = RequestMethod.POST)
    public ResponseEntity<String> saveShopSetting(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "settingName", "请求报文中未包含settingName");
        Assert.hasKeyAndValue(reqJson, "settingType", "请求报文中未包含settingType");
        Assert.hasKeyAndValue(reqJson, "settingKey", "请求报文中未包含settingKey");
        Assert.hasKeyAndValue(reqJson, "settingValue", "请求报文中未包含settingValue");


        ShopSettingPo shopSettingPo = BeanConvertUtil.covertBean(reqJson, ShopSettingPo.class);
        return saveShopSettingBMOImpl.save(shopSettingPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopSetting/updateShopSetting
     * @path /app/shopSetting/updateShopSetting
     */
    @RequestMapping(value = "/updateShopSetting", method = RequestMethod.POST)
    public ResponseEntity<String> updateShopSetting(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "ssId", "请求报文中未包含ssId");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "settingName", "请求报文中未包含settingName");
        Assert.hasKeyAndValue(reqJson, "settingType", "请求报文中未包含settingType");
        Assert.hasKeyAndValue(reqJson, "settingKey", "请求报文中未包含settingKey");
        Assert.hasKeyAndValue(reqJson, "settingValue", "请求报文中未包含settingValue");


        ShopSettingPo shopSettingPo = BeanConvertUtil.covertBean(reqJson, ShopSettingPo.class);
        return updateShopSettingBMOImpl.update(shopSettingPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopSetting/deleteShopSetting
     * @path /app/shopSetting/deleteShopSetting
     */
    @RequestMapping(value = "/deleteShopSetting", method = RequestMethod.POST)
    public ResponseEntity<String> deleteShopSetting(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "ssId", "ssId不能为空");


        ShopSettingPo shopSettingPo = BeanConvertUtil.covertBean(reqJson, ShopSettingPo.class);
        return deleteShopSettingBMOImpl.delete(shopSettingPo);
    }

    /**
     * 微信删除消息模板
     *
     * @return
     * @serviceCode /shopSetting/queryShopSetting
     * @path /app/shopSetting/queryShopSetting
     */
    @RequestMapping(value = "/queryShopSetting", method = RequestMethod.GET)
    public ResponseEntity<String> queryShopSetting(@RequestParam(value = "shopId") String shopId,
                                                   @RequestParam(value = "settingType", required = false) String settingType,
                                                   @RequestParam(value = "settingName", required = false) String settingName,
                                                   @RequestParam(value = "settingKey", required = false) String settingKey,
                                                   @RequestParam(value = "page") int page,
                                                   @RequestParam(value = "row") int row) {
        ShopSettingDto shopSettingDto = new ShopSettingDto();
        shopSettingDto.setSettingKey(settingKey);
        shopSettingDto.setSettingName(settingName);
        shopSettingDto.setSettingType(settingType);
        shopSettingDto.setShopId(shopId);
        shopSettingDto.setPage(page);
        shopSettingDto.setRow(row);
        return getShopSettingBMOImpl.get(shopSettingDto);
    }
}
